﻿// JavaScript Document

function Register()
{
	
	if(CheckUsername() && CheckPwd() && CheckPwd2() &&CheckEmail()&& CheckQQ() && CheckCellphone()&&CheckAuthCode() )
	{
	  var username=$('txtUsername').value;
	  var pwd=hex_md5($('txtPwd').value);
	  var qq=$('QQ').value;
	  var cellphone=$('cellphone').value;
	  var sex = $('sex').value;
	  var emot = $('emot').value;
	  var college = $('college').value;
	  var regbirthyear=$('regbirthyear').value;
	  var regbirthmonth=$('regbirthmonth').value;
	  var regbirthday=$('regbirthday').value;

	  var url="user/user.php?action=AddUser";	  
	  var para="username="+username;
	  para+="&password="+pwd;	
	  para+="&qq="+qq;
	  para+="&cellphone="+cellphone;
	  para+="&sex="+sex;
	  para+="&emot="+emot;
	  para+="&college="+college;	  
	  para+="&year="+regbirthyear;
	  para+="&month="+regbirthmonth;
	  para+="&day="+regbirthday;
	  response=SendPostRequest(url,para); 	  
	  //alert(response);
	  if(response!="false")
	  {	
		//写入cookie
		document.cookie="username"+"="+username+";path=/;";
		document.cookie="userID"+"="+response+";path=/;";
		if (username!=null || username!="")
		{
			alert('Welcome again '+username+'! 每天都登陆的话 会增加您的活跃度 但是一天之中反复登陆是没有用的哦')
		}
		else
		{
			alert('???');
		}
		//跳转到首页
		window.location.href="user.html";

		return false;
	  }
	  else
		alert('注册失败 请重新注册');
	}
}

function CheckUsername()
{
	var txtUsername=document.getElementById('txtUsername');
	var username=txtUsername.value;
	if(username==null ||username=="")
	{
		$('nameWarn').innerHTML="用户名不能为空!";
		return false ;
	}
	else
	{
	  var url="user.php?action=CheckUser";	  
	  var para="username="+username;
	  response=SendPostRequest(url,para); 	
	  if(response=="false")
	  {
		 $('nameWarn').innerHTML="用户名已存在，请用其他用户名!";
		 return false ;
	  }
	  else
	  {
		  $('nameWarn').innerHTML="ok";
		   return true;
	  }
	 
	  
	}
}

function CheckPwd()
{
	var pwd=document.getElementById('txtPwd').value;
	if(pwd==null || pwd=="")
	{
		$('pwdWarn').innerHTML="密码不能为空!";
		return false ;
	}
	else
	{
		$('pwdWarn').innerHTML="ok";
	    return true;
	}
}

function CheckPwd2()
{
	var pwd=document.getElementById('txtPwd').value;
	var pwd2=document.getElementById('txtPwd2').value;
	if(pwd2==null || pwd2=="")
	{	
	   $('pwd2Warn').innerHTML="密码不能为空!";	
		return false;
	}
	if(pwd2!=pwd)
	{	
	   $('pwd2Warn').innerHTML="密码不一致!";	
		return false;
	}
	else
	{
		$('pwd2Warn').innerHTML="ok";
		return true;
	}
	
}

function CheckEmail()
{
	var email=document.getElementById('txtEmail').value;
	
	if(email==null||email=="")
	{
		$('emailWarn').innerHTML="邮箱不能为空!";
		return false;
	}
	var myReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	if(!myReg.test(email))
	{
		$('emailWarn').innerHTML="请输入有效的Email!";
		return false;		
	}
	
	$('emailWarn').innerHTML="OK!";
	return true;

}
function CheckQQ()
{
	var QQ=document.getElementById('QQ').value;
    var patrn=/^[0-9]{1,20}$/;
	if(QQ==null)
	{
		$('QQWarn').innerHTML="请输入qq号!";	
		return false;		
	}
    else if(!patrn.exec(QQ))
    {
		$('QQWarn').innerHTML="请输入正确的qq号!";
		return false;
    } 
	else
	{
		$('QQWarn').innerHTML="OK!";
		return true;	
	}
}

function CheckCellphone()
{
	var phone=document.getElementById('cellphone').value;
	//alert(phone.length);
    var patrn=/^[0-9]{1,20}$/;
	if(phone==null)
	{
		$('cellphoneWarn').innerHTML="请输入手机号码!";	
		return false;		
	}
    else if(!patrn.exec(phone))
    {
		$('cellphoneWarn').innerHTML="请输入正确的手机号!";
		return false;
    } 
    else if(phone.length!=11)
    {
		$('cellphoneWarn').innerHTML="朋友 手机号码 应该是11位吧";
		return false;
    } 
	else
	{
		$('cellphoneWarn').innerHTML="OK!";
		return true;	
	}
}


var authCode;
function CheckAuthCode()
{
	var authWarn=document.getElementById('authWarn');
	if(document.getElementById('txtAuthCode').value!=authCode)
	{
		authWarn.innerHTML="验证码有误！";
		return false;
	}
	authWarn.innerHTML="OK";
	return true;
}

function GetAuthCode()
{	
	 var url="user/authCode.php";	  
	 var para=null;
	 var res=SendPostRequest(url,para); 
	 var resList=res.split('_');
	 var fileName=resList[0];
	 authCode=resList[1];
	// alert(fileName+";"+authCode);
	var authCodeImg=document.getElementById('authCodeImg');
	authCodeImg.src="user/"+fileName;
}
